//
//  DKUILabel+CustomText.m
//  DanKU
//
//  Created by EDY on 2023/10/29.
//

#import "DKUILabel+CustomText.h"

@implementation UILabel (CustomText)


- (void)addColoredText:(NSString *)text withColor:(UIColor *)color andFont:(UIFont *)font {
    // 创建一个NSMutableAttributedString来设置文本样式
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];

    // 找到文本中匹配的子字符串的范围
    NSRange range = [self.text rangeOfString:text];

    if (range.location != NSNotFound) {
        // 设置指定范围内的文本颜色和字体
        [attributedText addAttribute:NSForegroundColorAttributeName value:color range:range];
        [attributedText addAttribute:NSFontAttributeName value:font range:range];

        // 将NSAttributedString应用到UILabel上
        self.attributedText = attributedText;
    }
}

@end

